﻿#region 命名空间

using System;
using System.Web;
using System.Web.Http;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.WebUI.Models;

#endregion

namespace WinnerCMS.WebUI.Controllers.Api
{
    public class UserController : ApiController
    {
        #region 会员登录

        /// <summary>
        ///     会员登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public Result Login(LoginViewModel model)
        {
            var context = HttpContext.Current;

            var res = new Result();
            var config = CMSConfig.Instance();
            if (config.Member.IsPassCode && context.Session["VNum"] == null)
            {
                res.Errors = "验证码已经过期";
                return res;
            }
            if (config.Member.IsPassCode && model.PassCode != Convert.ToString(context.Session["VNum"]))
            {
                res.Errors = "验证码不正确";
                return res;
            }


            model.LastLoginIP = Common.HttpClient.GetIP();
            string err = null;
            new Public_Member(false).TryLogin(new User()
            {
                UserName = model.UserName,
                PassWord = model.PassWord,
                PassCode = model.PassCode,
                LastLoginIP = model.LastLoginIP
            }, ref err);
            res.Errors = err;

            return res;
        }

        #endregion

        #region 退出登录

        /// <summary>
        ///     退出登录
        /// </summary>
        /// <returns></returns>
        public Result Logout()
        {
            var res = new Result();
            try
            {
                new Public_Member(false).MemberLogout(UserManage.Current.UserName);
            }
            catch (Exception ex)
            {
                res.Errors = ex.Message;
            }
            return res;
        }

        #endregion

        #region 显示会员登录界面

        /// <summary>
        ///     显示会员登录界面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public dynamic ShowUserLogin()
        {
            return new MemberManage().ShowUserLogin();
        }

        #endregion

        #region 检查用户名是否存在

        /// <summary>
        ///     检查用户名是否存在
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        [HttpGet]
        public bool CheckUserName(string username)
        {
            return new MemberManage().CheckUserName(username);
        }

        #endregion
    }
}